package com.cong.leetcode;
class T implements Runnable {
    November november = new November();

    @Override
    public void run() {
        november = new November();
    }
}
public class November {
    public String sortString(String s) {
        if (s.length() <= 0)
            return "";

        StringBuilder sb = new StringBuilder(s);
        StringBuilder res = new StringBuilder();

        // 找到最小的字符，此时长度大于0
        while (true) {
            int indexOfLeast = 0;
            for (int i = 0; i < sb.length(); i++) {
                indexOfLeast = sb.charAt(i) <= sb.charAt(indexOfLeast) ? i : indexOfLeast;
            }
            res.append(sb.charAt(indexOfLeast));
            sb.deleteCharAt(indexOfLeast);
            // 如果sb没有了，则直接返回
            if (sb.length() == 0)
                return res.toString();
            // 找到sb中次最小的字符，此时必有字符, 但有可能无法继续寻找
            int indexOfSubLeast = -1;
            do {
                indexOfSubLeast = -1;
                for (int i = 0; i < sb.length(); i++) {
                    if (indexOfSubLeast == -1) {
                        if (sb.charAt(i) > res.charAt(res.length() - 1))
                            indexOfSubLeast = i;
                    } else {
                        if (sb.charAt(i) < sb.charAt(indexOfSubLeast) && sb.charAt(i) > res.charAt(res.length() - 1))
                            indexOfSubLeast = i;
                    }
                }
                if (indexOfSubLeast != -1) {
                    res.append(sb.charAt(indexOfSubLeast));
                    sb.deleteCharAt(indexOfSubLeast);
                }
            } while (indexOfSubLeast != -1);
            if (sb.length() == 0)
                return res.toString();
            // 从s中选出最大的字符接在字符串的后面 此时字符串长度大于 0
            int indexOfMax = 0;
            for (int i = 0; i < sb.length(); i++)
                indexOfMax = sb.charAt(i) > sb.charAt(indexOfMax) ? i : indexOfMax;
            res.append(sb.charAt(indexOfMax));
            sb.deleteCharAt(indexOfMax);
            if (sb.length() == 0)
                return res.toString();
            // 从 剩余字符中选出次大的字符
            int indexOfSubMax = -1;
            do {
                indexOfSubMax = -1;
                for (int i = 0; i < sb.length(); i++) {
                    if (indexOfSubMax == -1) {
                        if (sb.charAt(i) < res.charAt(res.length() - 1))
                            indexOfSubMax = i;
                    } else {
                        if (sb.charAt(i) > sb.charAt(indexOfSubMax) && sb.charAt(i) < res.charAt(res.length() - 1))
                            indexOfSubMax = i;
                    }
                }
                if (indexOfSubMax != -1) {
                    res.append(sb.charAt(indexOfSubMax));
                    sb.deleteCharAt(indexOfSubMax);
                }
            } while (indexOfSubMax != -1);
            if (sb.length() == 0)
                return res.toString();
        }

    }

    int t;
    November() {
        t = 999999;
        for (int i = 0;i<1000000;i++) {
            t = i;
        }
//        System.out.println('.');
//        try {
//            Thread.sleep(1);
//        }catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }

    public static void main(String[] args) {
        November n = new November();
        
        T t = new T();
        for(int i = 0;i < 100000;i++) {
            new Thread(t).start();
//            System.out.println(t.november.t);
            if (t.november.t != 999999) {
                System.out.println("TAG: " + t.november.t);
            }
        }
//        for (int i = 0;i < 1000000; i++) {
//            new Thread(() -> {
//                Singleton singleton = Singleton.getInstance();
//                if (singleton.v != 999) {
//                    System.out.println("fuck: " + singleton.v);
//                }
//            }).start();
//        }
    }
}

class Singleton {
    public static Singleton instance = null;
    int v;
    private Singleton() {
        System.out.println("single ton");
        v = 999;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static   Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                    return instance;
                }
            }
        }
        return instance;
    }
}
